/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.dto;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import org.gwtportlets.portlet.smartgwt.client.DataTransferObject;
import org.gwtportlets.portlet.smartgwt.client.SmartPortlet;
import org.gwtportlets.portlet.smartgwt.client.SmartPortletDataSource;

/**
 *
 */
public class StackTraceElementDto implements DataTransferObject {
    public String id;
    public String failureId;
    public String threadId;
    public int index;
    public String fileName;
    public int lineNumber;
    public String className;
    public String methodName;
    public static final String DATA_SOURCE_ID = "stackTraceElementDS";

    public static SmartPortletDataSource getDataSource(SmartPortlet portlet) {
        return getDataSource(portlet, DATA_SOURCE_ID);
    }

    public static SmartPortletDataSource getDataSource(SmartPortlet portlet, String id) {
        id = portlet.getDataSourceId(id);
        SmartPortletDataSource ds = (SmartPortletDataSource) DataSource.getDataSource(id);
        if (ds != null) {
            return ds;
        }
        ds = new SmartPortletDataSource(portlet);
        ds.setID(id);
        DataSourceField field;

        field = new DataSourceTextField("id", "Id");
        field.setPrimaryKey(true);
        field.setRequired(true);
        ds.addField(field);

        field = new DataSourceTextField("failureId", "Failure Id");
        field.setForeignKey("failureDS.id");
        field.setRequired(true);
        ds.addField(field);

        field = new DataSourceTextField("threadId", "Thread Id");
        field.setForeignKey("threadDS.id");
        field.setRequired(true);
        ds.addField(field);

        field = new DataSourceIntegerField("index", "Index");
        field.setRequired(true);
        ds.addField(field);

        field = new DataSourceTextField("fileName", "FileName");
        field.setRequired(true);
        ds.addField(field);

        field = new DataSourceIntegerField("lineNumber", "Line Number");
        field.setRequired(true);
        ds.addField(field);

        field = new DataSourceTextField("className", "Class Name");
        field.setRequired(true);
        ds.addField(field);

        field = new DataSourceTextField("methodName", "Method Name");
        field.setRequired(true);
        ds.addField(field);

        return ds;
    }

    public void copyToRecord(Record record) {
        record.setAttribute("id", id);
        record.setAttribute("failureId", failureId);
        record.setAttribute("threadId", threadId);
        record.setAttribute("index", index);
        record.setAttribute("fileName", fileName);
        record.setAttribute("lineNumber", lineNumber);
        record.setAttribute("className", className);
        record.setAttribute("methodName", methodName);
    }

    public void copyFromRecord(Record from) {
    }
}
